package com.candoit.webapps.pricing.bean.adapters;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.FacesConverter;
import javax.persistence.EntityManager;

import com.candoit.webapps.pricing.bean.PricingContext;
import com.candoit.webapps.pricing.model.Maquina;

@FacesConverter(value="maquinaConverter")
public class MaquinaConverter extends GenericConverter {

	@Override
	public Object getAsObject(FacesContext context, UIComponent component,
			String value) {
		EntityManager em = PricingContext.getInstance().getEntityManager();
		String query = "select m from Maquina m  where m.id= :id";
		return em.createQuery(query).setParameter("id" , Integer.parseInt(value)).getSingleResult();
	}

	@Override
	public String getAsString(FacesContext context, UIComponent component,
			Object value) {
		int id = ((Maquina)value).getId();
		return (String.valueOf(id));
	}

}
